#Region " Imports "

Imports System
Imports System.Data
Imports BlazeApps.Library.Search

#End Region

Partial Class Usercontrols_search
    Inherits UserControlBase

#Region " Enums, Memebers and Properties "

    Private DataPageLocal As New PagedDataSource

#End Region

#Region " Page Events "

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        SetPageTitle("Search Results")
        Me.uxSearchTextBox.Text = GetStringFromSession("searchtext")

        If Me.Page.IsPostBack = False Then
            LoadPage()
        End If

        ConfigBaseNavCtrls()

        If Not Me.uxSearchTextBox.Text = String.Empty Then
            BindDataItems()
        End If

        If GetBooleanFromQueryString("tagsearch") = True Then
            Me.uxSearchTextBox.Text = GetStringFromQueryString("tagname")
            BindDataItems()
        End If

        Me.uxSearchTimeLabel.Text = AddPageTimer(Resources.Resource.SearchTime)

    End Sub

#End Region

#Region " Save Data "



#End Region

#Region " Load Data "

    Private Sub LoadPage()
        LoadPageSize(Me.uxPageSizeDropDownList)
    End Sub

    Public Overloads Overrides Sub BindDataItems()

        Dim DSR As DataSet
        Dim Results As New PageSearch
        Dim SearchText As String

        If GetBooleanFromQueryString("tagsearch") = True Then

            If String.Compare(Me.uxSearchTextBox.Text, GetStringFromQueryString("tagname")) < 0 Then
                SearchText = Me.uxSearchTextBox.Text.Trim
            Else
                SearchText = GetStringFromQueryString("tagname")
            End If

            DSR = Results.GetPageResultsByTag(SearchText, BuildItemTypeClause)
        Else
            DSR = Results.GetPageResults(Me.uxSearchTextBox.Text.Trim, BuildItemTypeClause)
        End If

        Me.RecordCount = DSR.Tables(0).Rows.Count

        Me.DataPage.DataSource = DSR.Tables(0).DefaultView
        Me.DataPage.AllowPaging = True
        Me.DataPage.PageSize = CType(Me.uxPageSizeDropDownList.SelectedValue.ToString, Integer)
        Me.DataPage.CurrentPageIndex = Me.PageNumber

        CreatePagingLinks()

        With Me.uxResultsRepeater
            .DataSource = Me.DataPage
            .DataBind()
        End With

    End Sub

#End Region

#Region " Form Controls Events "

    Protected Sub uxSubmitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxSubmitButton.Click
        Session.Add("searchtext", Me.uxSearchTextBox.Text.Trim)
        BindDataItems()
    End Sub

    Protected Sub uxResetButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxResetButton.Click
        Me.uxSearchTextBox.Text = ""
        Session.Add("searchtext", "")

        With Me.uxResultsRepeater
            .DataSource = Nothing
            .DataBind()
        End With
    End Sub

    Protected Sub uxResultsRepeater_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles uxResultsRepeater.ItemDataBound

        Dim PNL As Panel = CType(e.Item.FindControl("uxActivePreviewPanel"), Panel)

        If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then

            If e.Item.DataItem("linktype") = BlazeApps.Library.Base.ItemType.Resources Then
                PNL.Visible = False
                PNL.Enabled = False
            Else
                PNL.Visible = True
                PNL.Enabled = True
            End If

            e.Item.Visible = ShowItem(ConvertToGuid(e.Item.DataItem("itemid").ToString))
        End If

    End Sub

#End Region

#Region " Helpers and Methods "

    Private Sub ConfigBaseNavCtrls()

        Me.PagingTopPanel = Me.uxPagingTopPanel
        Me.PagingBottomPanel = Me.uxPagingBottomPanel

        Me.RepeaterTop = Me.uxPagesTopRepeater
        Me.RepeaterBottom = Me.uxPagesBottomRepeater

        Me.NextTopLinkButton = Me.uxTopNextLinkButton
        Me.PreviousTopLinkButton = Me.uxTopPrevLinkButton
        Me.NextBottomLinkButton = Me.uxBottomNextLinkButton
        Me.PreviousBottomLinkButton = Me.uxBottomPrevLinkButton

        Me.DataPage = DataPageLocal
        Me.PageSize = CType(Me.uxPageSizeDropDownList.SelectedValue.ToString, Integer)
        Me.PagesToDisplay = 10

        Me.ItemsPerPageDropDown = Me.uxPageSizeDropDownList
        Me.ItemsPerPageLabel = Me.uxItemsPerPageLabel
        Me.PagerInfoLabel = Me.uxPagerInfoLabel

        AddHandler Me.RepeaterTop.ItemCommand, AddressOf PagingRepeater_ItemCommand
        AddHandler Me.RepeaterBottom.ItemCommand, AddressOf PagingRepeater_ItemCommand

    End Sub

    Private Function BuildTagWhereClause(ByVal SearchTags As String) As String
        Return String.Format("tagname = '{0}'", SearchTags)
    End Function

    Private Function BuildWhereClaue(ByVal SearchString As String) As String
        Return String.Format("(title like '%{0}%' OR summary like '%{0}%' OR body like '%{0}%')", SearchString)
    End Function

    Private Function BuildItemTypeClause() As String
        Return String.Format("1,2,3,4,5,6,7")
    End Function

#End Region

End Class
